package com.dream.technology.utils.parse;



import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;


public class ObjectUtil {

    private static ObjectMapper objectMapper = new ObjectMapper();




    /**
     * 方法通过测试，会转换成 linkedhashmap嵌套的形式
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> objToMap(Object obj) {

        return objectMapper.convertValue(obj, Map.class);
    }


    public static Integer valueConvertToInt(Object term) {
        if (null == term) {
            return 0;
        }

        return Integer.valueOf(term.toString());
    }

    public static BigDecimal valueConvertBigDecimal(Object src_value) {

        if (null == src_value) {
            return null;
        }

        return new BigDecimal(src_value.toString());
    }


    public static Double convertToDouble(Object src_value) {

        if (null == src_value) {
            return null;
        }
        return Double.valueOf(src_value.toString());
    }


    /**
     * 获取collection的莫一个属性，放入set中并返回
     *
     * @param src
     * @param keyField
     * @param fieldType
     * @param <T>
     * @param <V>
     * @return
     */
    public static <T, V> Set<V> toFieldSet(Collection<T> src, String keyField, Class<V> fieldType) {

        Set<V> des = new HashSet<>();
        for (T temp : src) {
            V oneFildValue = getObjField(temp, keyField, fieldType);
            des.add(oneFildValue);
        }
        return des;
    }

    /**
     * 获取对象中的属性
     *
     * @param temp
     * @param keyField
     * @param fieldType
     * @param <V>
     * @param <T>
     * @return
     */
    private static <V, T> V getObjField(T temp, String keyField, Class<V> fieldType) {

        //public属性获取
        try {
            Field field = temp.getClass().getField(keyField);
            return (V) field.get(temp);
        } catch (Exception e) {

        }


        //get方法获取
        String getMethodName = "get" + keyField.substring(0, 1).toUpperCase() + keyField.substring(1);
        try {
            Method methodGet = temp.getClass().getMethod(getMethodName);
            return (V) methodGet.invoke(temp);
        } catch (Exception e) {
            throw new RuntimeException("Error in called method ： " + temp.getClass().getCanonicalName() + "." + getMethodName + "()", e);

        }


    }


    /**
     * 將clooection 转化为map,按照某一个属性进行转换，作为key
     *
     * @param srcCollec
     * @param fildName
     * @param fieldType
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, List<V>> collecToFildMap(Collection<V> srcCollec, String fildName, Class<K> fieldType) {

        Map<K, List<V>> map = new HashMap<>();
        for (V temp : srcCollec) {
            K fieldValue = getObjField(temp, fildName, fieldType);
            List<V> existList = map.get(fieldValue);
            if (null == existList) {
                existList = new ArrayList<>();
                map.put(fieldValue, existList);
            }
            existList.add(temp);

        }
        return map;

    }

    /**
     * 属性拷贝，必须有get，set方法
     *
     * @param src
     * @param des
     */
    public static void copyProPerty(Object src, Object des) {
        if (null == src || null == des) {
            throw new RuntimeException("Argument can't be null");
        }
        Class<?> srcClass = src.getClass();
        Class<?> desClass = des.getClass();

        Method[] desmethods = desClass.getMethods();
        for (Method desMethod : desmethods) {
            String methodName = desMethod.getName();
            if (methodName.length() <= 3) {
                continue;
            }
            String first = methodName.substring(0, 3);
            String end = methodName.substring(3);

            if (first.equals("set")) {
                try {
                    Method srcGetMethod = srcClass.getMethod("get" + end, new Class[]{});
                    desMethod.invoke(des, srcGetMethod.invoke(src, new Object[]{}));

                } catch (Exception e) {

                }
            }
        }


    }

    public static void main(String[] args) {


        test1();


    }

    private static void test1() {
        BigDecimal kk = new BigDecimal(1.2);
        System.out.println(kk.toString());
        Double dd = Double.valueOf(kk.toString());
        System.out.println(dd);
    }



}
